package org.bluedream.core.module.dao.stock;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.bluedream.core.module.entity.stock.SkuStock;
import org.bluedream.core.module.entity.stock.TmpSkuStock;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName SkuStockDao
 * @Description TODO
 * @Author foxsand
 * @Data 10:31
 * @Version
 */
public interface SkuStockDao extends BaseMapper<SkuStock> {
    @Override
    @Select("select skuStock.code as code , b.name as name , b.barcode as barcode ,  skuStock.war_code as warCode , c.name as wareName , skuStock.qty as qty , skuStock.oprice as oprice , skuStock.total as total , " +
            " skuStock.id as id , skuStock.create_by as createBy , skuStock.create_date as createDate , skuStock.update_by as updateBy , skuStock.update_date as updateDate , skuStock.status as status , skuStock.remarks as remarks  " +
            "from sku_stock skuStock , goods b , warehouse c " +
            "${ew.customSqlSegment} and skuStock.code = b.code and skuStock.war_code = c.code and skuStock.org_code = b.org_code and skuStock.org_code = c.org_code")
    IPage<SkuStock> selectPage(IPage<SkuStock> page, @Param("ew")Wrapper<SkuStock> queryWrapper);

    @Override
    @Select("select a.code as code , b.name as name , b.barcode as barcode ,  a.war_code as warCode , c.name as wareName , a.qty as qty , a.oprice as oprice , a.total as total , " +
            " a.id as id , a.create_by as createBy , a.create_date as createDate , a.update_by as updateBy , a.update_date as updateDate , a.status as status , a.remarks as remarks  " +
            "from sku_stock a , goods b , warehouse c " +
            "${ew.customSqlSegment} and a.code = b.code and a.war_code = c.code")
    List<SkuStock> selectList(@Param("ew")Wrapper<SkuStock> queryWrapper);

    @Select("select sum(qty) as qty from (" +
            "select org_code , war_code , code , decode(bill_type_label , '0' , -1*qty , '1' , qty) as qty , decode(bill_type_label , '0' , -1*total , '1' , total) as total " +
            "       from tmpSkuStock where org_code = #{orgCode} and war_code = #{warCode} and code = #{code} " +
            "union all " +
            "select org_code , war_code , code , qty , total " +
            "       from   sku_stock  where org_code = #{orgCode} and war_code = #{warCode} and code = #{code} " +
            "       ) a")
    BigDecimal getNowSkuStockQty(@Param("orgCode")String loginOrgCode , @Param("warCode")String warCode , @Param("code")String code);

    @Select("select sum(qty) as sumQty , sum(total) as sumTotal from (" +
            "select org_code , war_code , code , decode(bill_type_label , '0' , -1*qty , '1' , qty) as qty , decode(bill_type_label , '0' , -1*total , '1' , total) as total " +
            "       from tmpSkuStock where org_code = #{orgCode} and war_code = #{warCode} and code = #{code} " +
            "union all " +
            "select org_code , war_code , code , qty , total " +
            "       from   sku_stock  where org_code = #{orgCode} and war_code = #{warCode} and code = #{code} " +
            "       ) a")
    TmpSkuStock getProductSumData(@Param("orgCode")String loginOrgCode , @Param("warCode")String warCode , @Param("code")String code);
}
